import os
import xml.dom.minidom
import cv2
from openpyxl import Workbook,load_workbook


def getFileList(dir, Filelist, ext=None):
    """
    获取文件夹及其子文件夹中文件列表
    输入 dir：文件夹根目录
    输入 ext: 扩展名
    返回： 文件路径列表
    """
    newDir = dir
    if os.path.isfile(dir):
        if ext is None:
            Filelist.append(dir)
        else:
            if ext in dir[-3:]:
                Filelist.append(dir)

    elif os.path.isdir(dir):
        for s in os.listdir(dir):
            newDir = os.path.join(dir, s)
            getFileList(newDir, Filelist, ext)
    return Filelist

base_PATH=os.getcwd()
PATH_images = './limite50_face'  # 当前文件路径

wb=load_workbook('size_limite50_face.xlsx')
sn=wb.sheetnames
ws = wb[sn[0]]

# 检索文件
imglist = getFileList(PATH_images, [], 'png')
print('本次执行检索到 '+str(len(imglist))+' 张图像\n')
id=1
for imgpath in imglist:
    #imgname = os.path.splitext(os.path.basename(imgpath))[0]
    img = cv2.imread(imgpath)
    _size = img.shape
    width = _size[1]
    height = _size[0]
    ws.cell(row=id, column=1).value = width
    ws.cell(row=id, column=2).value = height
    id+=1

os.chdir(base_PATH)
wb.save('size_limite50_face.xlsx')